package com.polidea.rxohosble2.internal.connection;

import bleshadow.javax.inject.Inject;
import com.polidea.rxohosble2.HiddenBluetoothGattCallback;
import ohos.bluetooth.ble.BlePeripheralCallback;
import ohos.bluetooth.ble.BlePeripheralDevice;
import ohos.bluetooth.ble.GattCharacteristic;
import ohos.bluetooth.ble.GattDescriptor;

class NativeCallbackDispatcher {

    private BlePeripheralCallback nativeCallback;
    private HiddenBluetoothGattCallback nativeCallbackHidden;

    @Inject
    NativeCallbackDispatcher() {

    }

    void notifyNativeChangedCallback(BlePeripheralDevice gatt, GattCharacteristic characteristic) {
        if (nativeCallback != null) {
            nativeCallback.characteristicChangedEvent(characteristic);
        }
    }

    void notifyNativeConnectionStateCallback(BlePeripheralDevice gatt, int state, int newState) {
        if (nativeCallback != null) {
            nativeCallback.connectionStateChangeEvent(newState);
        }
    }

    void notifyNativeDescriptorReadCallback(BlePeripheralDevice gatt, GattDescriptor descriptor, int status) {
        if (nativeCallback != null) {
            nativeCallback.descriptorReadEvent(descriptor, status);
        }
    }

    void notifyNativeDescriptorWriteCallback(BlePeripheralDevice gatt, GattDescriptor descriptor, int status) {
        if (nativeCallback != null) {
            nativeCallback.descriptorWriteEvent(descriptor, status);
        }
    }

    void notifyNativeMtuChangedCallback(BlePeripheralDevice gatt, int mtu, int status) {
        if (nativeCallback != null) {
            nativeCallback.mtuUpdateEvent(mtu, status);
        }
    }

    void notifyNativeReadRssiCallback(BlePeripheralDevice gatt, int rssi, int status) {
        if (nativeCallback != null) {
            nativeCallback.readRemoteRssiEvent(rssi, status);
        }
    }

    void notifyNativeReliableWriteCallback(BlePeripheralDevice gatt, int status) {
        if (nativeCallback != null) {
//            nativeCallback.onReliableWriteCompleted(gatt, status);
        }
    }

    void notifyNativeServicesDiscoveredCallback(BlePeripheralDevice gatt, int status) {
        if (nativeCallback != null) {
            nativeCallback.servicesDiscoveredEvent(status);
        }
    }

    void notifyNativeWriteCallback(BlePeripheralDevice gatt, GattCharacteristic characteristic, int status) {
        if (nativeCallback != null) {
            nativeCallback.characteristicWriteEvent(characteristic, status);
        }
    }

    void notifyNativeParamsUpdateCallback(BlePeripheralDevice gatt, int interval, int latency, int timeout, int status) {
        if (nativeCallbackHidden != null) {
            nativeCallbackHidden.onConnectionUpdated(gatt, interval, latency, timeout, status);
        }
    }

    void setNativeCallback(BlePeripheralCallback callback) {
        this.nativeCallback = callback;
    }

    void notifyNativeReadCallback(BlePeripheralDevice gatt, GattCharacteristic characteristic, int status) {
        if (nativeCallback != null) {
            nativeCallback.characteristicReadEvent(characteristic, status);
        }
    }

    void setNativeCallbackHidden(HiddenBluetoothGattCallback callbackHidden) {
        this.nativeCallbackHidden = callbackHidden;
    }
}
